//
//  STCalendarKitMonthManger.m
//  STNewTools
//
//  Created by stoneobs on 2017/7/20.
//  Copyright © 2017年 stoneobs. All rights reserved.
//

#import "STCalendarKitMonthManger.h"
#import "NSCalendar+STCalendarTool.h"
#import "NSDate+STFormatter.h"
@implementation STCalendarKitMonthManger
+ (STCalendarKitMonthModel *)defult{
    
    static STCalendarKitMonthManger * defult = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        defult = [STCalendarKitMonthManger new];
    });
    return defult;
}

- (STCalendarKitMonthModel *)calculateMonthModelFromDate:(NSDate *)date{
    
    NSDate * todayDate = [NSDate localCurrentDate];
    NSDateComponents * todayCom = [NSCalendar comWithDate:todayDate];
    
    STCalendarKitMonthModel * monthModel = [STCalendarKitMonthModel new];
    
    monthModel.allDaysArray= [[NSMutableArray alloc] initWithCapacity:42];
    monthModel.currentMonthDate = date;
    
    //这个月的1号是周几
    NSInteger  firstWeekDay = [NSCalendar whatTheMonthBeginInWeekByDate:date];
    //适配到中国的周计算
    firstWeekDay = firstWeekDay - 1;
    firstWeekDay = firstWeekDay?firstWeekDay:7;
    //获取上一个月在本月的数量
    NSInteger lastMonthNum = firstWeekDay - 1;
    //获取上一个月有多少天
    NSInteger lastMonthDays = [NSCalendar numOfMonthDaysByDate:[NSCalendar lastMonthDate:date]];
    //-----构造上月的模型
    for (int i = 0; i < lastMonthNum; i++) {
        STCalendarKitDayModel * dayModel = [STCalendarKitDayModel new];
        dayModel.isToday = NO;
        dayModel.isLastMonth = YES;
        dayModel.isThisMonth = NO;
        dayModel.isNextMonth = NO;
        dayModel.monthDate = date;
        dayModel.dayValue = lastMonthDays - (lastMonthNum - i) + 1;
        dayModel.isExistSchedule = NO;
        //获取上个月日历
        NSDateComponents * com = [NSCalendar comWithDate:[NSCalendar lastMonthDate:date]];
        com.day = lastMonthDays - (lastMonthNum - i) + 1;
        
        dayModel.date = [NSCalendar dateFromCom:com];
        
        [monthModel.allDaysArray addObject:dayModel];
        
        
        
    }
    //------构造本月的模型
    //获取今日是这个月的第几天
    NSInteger todayIndex = [NSCalendar indexOfTheMonthDate:date];
    //获取这个月有多少天
    NSInteger thisMonthDays = [NSCalendar numOfMonthDaysByDate:date];
    for (int i = 0; i< thisMonthDays; i++) {
        
        STCalendarKitDayModel * dayModel = [STCalendarKitDayModel new];
        dayModel.isToday = NO;
        dayModel.isLastMonth = NO;
        dayModel.isThisMonth = YES;
        dayModel.isNextMonth = NO;
        dayModel.monthDate = date;
        dayModel.dayValue = i + 1;
        dayModel.isExistSchedule = NO;
        //获取这个月日历
        NSDateComponents * com = [NSCalendar comWithDate:date];
        com.day = i + 1;
        dayModel.date = [NSCalendar dateFromCom:com];
        //是否是今日
        if (i+1 == todayIndex && com.year == todayCom.year && com.month == todayCom.month) {
            dayModel.isToday = YES;
        }
        
        [monthModel.allDaysArray addObject:dayModel];
    }
    //---构造下一个月的模型
    //下个月显示数据还剩下多少天
    NSInteger nextMonthDays = 42 - monthModel.allDaysArray.count;
    //构造下个月的数据
    for (int i = 0; i< nextMonthDays; i++) {
        STCalendarKitDayModel * dayModel = [STCalendarKitDayModel new];
        dayModel.isToday = NO;
        dayModel.isLastMonth = NO;
        dayModel.isThisMonth = NO;
        dayModel.isNextMonth = YES;
        dayModel.monthDate = date;
        dayModel.dayValue = i + 1;
        dayModel.isExistSchedule = NO;
        //获取下个月日历
        NSDateComponents * com = [NSCalendar comWithDate:[NSCalendar nextMonthDate:date]];
        com.day = i + 1;
    
        dayModel.date = [NSCalendar dateFromCom:com];
        //
        
        [monthModel.allDaysArray addObject:dayModel];
    }
    
    return monthModel;
    
}
- (NSArray<STCalendarKitMonthModel*>*)calculateNextMonthsWithDate:(NSDate*)date
                                                             nums:(NSInteger)nums{
    
    NSMutableArray * array = [NSMutableArray new];
    NSDate * nextDate = [date copy];
    for (int i = 0; i< nums; i++) {
        
        
        STCalendarKitMonthModel * monthModel = [self calculateMonthModelFromDate:nextDate];
        nextDate = [NSCalendar nextMonthDate:nextDate];
        [array addObject:monthModel];
    }
    
    return [NSArray arrayWithArray:array];
}
- (NSArray<STCalendarKitMonthModel *> *)calculatelastMonthsWithDate:(NSDate *)date nums:(NSInteger)nums{
    NSMutableArray * array = [NSMutableArray new];
    NSDate * lastDate = [date copy];
    for (int i = 0; i< nums; i++) {
        
        lastDate = [NSCalendar lastMonthDate:lastDate];
        STCalendarKitMonthModel * monthModel = [self calculateMonthModelFromDate:lastDate];
        
        [array addObject:monthModel];
    }
    
    return [NSArray arrayWithArray:array];
    
}
@end
